﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Common.Utilities.OpenXml;

namespace Workflows.Components.Reports.OpenXmlUtil
{
	/// <summary>
	/// 
	/// </summary>
	[Serializable]
	public class TemplateReportInstruction:InstructionBase
	{
		#region props
		private string _TemplateFilePath;
		/// <summary></summary>
		public string TemplateFilePath
		{
			get
			{
				return this._TemplateFilePath;
			}
			set
			{
				this._TemplateFilePath = value;
			}
		}

		private string _OutputFilePath;
		/// <summary></summary>
		public string OutputFilePath
		{
			get
			{
				return this._OutputFilePath;
			}
			set
			{
				this._OutputFilePath = value;
			}
		}

		private Dictionary<string, WorksheetDataBinding> _DataBindings;
		/// <summary>data field key (worksheet!range) -> bindings</summary>
		public Dictionary<string, WorksheetDataBinding> DataBindings
		{
			get
			{
				return this._DataBindings;
			}
			set
			{
				this._DataBindings = value;
			}
		}

		private Dictionary<string, Dictionary<string, string>> _SummaryRowFomulars;
		/// <summary>
		/// data field key -> table column name -> formular
		/// can update formular by replacing place holders 
		/// ${TableRowStart}
		/// ${TableRowEnd}
		/// </summary>
		public Dictionary<string,Dictionary<string,string>> SummaryRowFomulars
		{
			get
			{
				return this._SummaryRowFomulars;
			}
			set
			{
				this._SummaryRowFomulars = value;
			}
		}

		private Dictionary<string,Dictionary<int,string>> _ColumnFormulars;
		/// <summary>data field key -> column position -> formular</summary>
		public Dictionary<string,Dictionary<int,string>> ColumnFormulars
		{
			get
			{
				return this._ColumnFormulars;
			}
			set
			{
				this._ColumnFormulars = value;
			}
		}

		private Dictionary<string,ChartDataBinding> _ChartBindings;
		/// <summary>chart field key (sheetname!chartname[series/category/title][series name]) -> formular</summary>
		public Dictionary<string,ChartDataBinding> ChartBindings
		{
			get
			{
				return this._ChartBindings;
			}
			set
			{
				this._ChartBindings = value;
			}
		}
		#endregion

		#region ctor
		/// <summary>
		/// 
		/// </summary>
		public TemplateReportInstruction():base(Guid.Empty,Guid.Empty)
		{
			this._TemplateFilePath = string.Empty;
			this._OutputFilePath = string.Empty;
			this._DataBindings=new Dictionary<string, WorksheetDataBinding>();
			this._SummaryRowFomulars=new Dictionary<string, Dictionary<string, string>>();
			this._ColumnFormulars=new Dictionary<string, Dictionary<int, string>>();
			this._ChartBindings=new Dictionary<string, ChartDataBinding>();
		}

		/// <summary>
		/// constructor, all derived class must call default constructor to initialize
		/// default settings.
		/// </summary>
		public TemplateReportInstruction(Guid componentID, Guid jobTicketID,
			string templatePath, string outputPath,
			Dictionary<string,WorksheetDataBinding> dataBindings,
			Dictionary<string,Dictionary<string,string>> rowFormulars,
			Dictionary<string,Dictionary<int,string>> colFormulars,
			Dictionary<string,ChartDataBinding> chartBindings) 
			: base(componentID, jobTicketID)
		{
			this._TemplateFilePath = templatePath;
			this._OutputFilePath = outputPath;
			this._DataBindings = dataBindings;
			this._SummaryRowFomulars = rowFormulars;
			this._ColumnFormulars = colFormulars;
			this._ChartBindings = chartBindings;
		}
		#endregion

		#region Overrides of InstructionBase

		/// <summary>
		/// serialize settings to xml
		/// the function below is mainly for storing into database as serialized form instead of string
		/// </summary>
		public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
		{
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TemplateFilePath", this.TemplateFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ReportFilePath", this.OutputFilePath);

		    return xNode;
		}

		/// <summary>
		/// instantiated from xml
		/// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
		/// instantiate from first child of root node
		/// </summary>
		public override InstructionBase Instantiate(XmlNode xNode)
		{
			throw new System.NotImplementedException();
		}

		#endregion
	}
}
